package com.yunsoft.util;

import java.util.Map;

import org.slf4j.Logger;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 极光推送工具类
 * @author yangyunfeng
 *
 */
public class JpushUtil {
	//请发送消息
	public static final int SEND_MESSAGE=1;

	
	
	public static JPushClient jpushClient = new JPushClient(Constant.JSPUSH_Master_Secret, Constant.JSPUSH_AppKey,null,ClientConfig.getInstance());
	private static Logger LOG = org.slf4j.LoggerFactory.getLogger(JpushUtil.class);
	
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 
	 * @param registrationId 接收者的注册id
	 * @param code 发送通知类型
	 * @param showContent 发送通知显示内容
	 * @param title 发送通知的标题
	 * @param NoticeDatas 发送通知的数据
	 * @param messageContent 附带的消息
	 */
	public static void sendNoticeMessage(String registrationId,int code,String showContent,String title,Map<String,String> NoticeDatas,String messageContent){
			
	//	String registrationId = "1a0018970a971e7fd0a";
		  PushPayload payload ;
		switch (code) {
		case SEND_MESSAGE:
			 payload = buildPushObject_add_friend(registrationId, NoticeDatas, showContent, title,messageContent);
			break;

		default:
			 payload = buildPushObject_add_friend(registrationId, NoticeDatas, showContent, title,messageContent);
			break;
		}

		try {
	        PushResult result = jpushClient.sendPush(payload);
	        LOG.info("Got result - " + result);
	        

	    } catch (APIConnectionException e) {
	        // Connection error, should retry later
	        LOG.error("Connection error, should retry later", e);

	    } catch (APIRequestException e) {
	        // Should review the error, and fix the request
	        LOG.error("Should review the error, and fix the request", e);
	        LOG.info("HTTP Status: " + e.getStatus());
	        LOG.info("Error Code: " + e.getErrorCode());
	        LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	
	

	public static PushPayload buildPushObject_add_friend(String registrationId,Map<String,String> datas,String showContent,String title,String messageContent) {
		// TODO Auto-generated method stub
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.registrationId(registrationId))
				.setNotification(Notification.android(showContent, title, datas)).setMessage(Message.content(messageContent)).build();
	}
	
	
	/**
	 * 发送消息 消息类别
	 * @param code
	 * @param registrationId 注册id
	 * @param extras map扩展
	 * @param messageContent 消息体
	 */
	public static void sendMessage(int code,String registrationId,Map<String,String> extras,String messageContent ){
		  PushPayload payload ;
			switch (code) {
			case SEND_MESSAGE:
				 payload = buildPushObject_message(registrationId,messageContent,extras);
				break;
			default:
				 payload = buildPushObject_message(registrationId,messageContent,extras);
				break;
			}

			try {
		        PushResult result = jpushClient.sendPush(payload);
		        LOG.info("Got result - " + result);
		        

		    } catch (APIConnectionException e) {
		        // Connection error, should retry later
		        LOG.error("Connection error, should retry later", e);

		    } catch (APIRequestException e) {
		        // Should review the error, and fix the request
		        LOG.error("Should review the error, and fix the request", e);
		        LOG.info("HTTP Status: " + e.getStatus());
		        LOG.info("Error Code: " + e.getErrorCode());
		        LOG.info("Error Message: " + e.getErrorMessage());
		    }
	}
	
	

	public static PushPayload buildPushObject_message(String registrationId,String messageContent,Map<String,String>  extras) {
		// TODO Auto-generated method stub
		return PushPayload.newBuilder().setPlatform(Platform.all())
				.setAudience(Audience.registrationId(registrationId))
				.setMessage(Message.newBuilder().setMsgContent(messageContent).addExtras(extras).build())
				.build();
	}
}
